<template>
  <CustomDialog :drag-dialog="dialogInfo">
    <el-form ref="form" :model="form" :disabled="!form.busPageInfo.editable" size="mini" label-width="160px">
      <el-form-item label="消息标题" prop="busMainData.msgTitle">
        <el-input v-model="form.busMainData.msgTitle" />
      </el-form-item>
      <el-form-item label="消息内容" prop="busMainData.msgContent">
        <el-input
          v-if="form.busPageInfo.editable"
          v-model="form.busMainData.msgContent"
          :autosize="{ minRows: 10 }"
          type="textarea"
          placeholder="请输入内容"
        />
        <p
          v-else
          v-html="form.busMainData.msgContent" />
      </el-form-item>
    </el-form>
    <div v-permission="['SYS_ADMIN','SYSMSG_ALL','SYSMSG_CREATE']" v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSave">发送</el-button>
    </div>
  </CustomDialog>
</template>

<script>
import sysMsg from '@/api/system/SysMsg'

export default {
  components: { },
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isAdd: false,
      loading: false,
      style: 'width: 184px',
      dialogInfo: {
        name: 'sysMsgMngFormDialog',
        title: '消息查看',
        fullScreen: false,
        width: 500,
        height: 70
      },
      form: {
        busMainData: {
          id: '',
          enableFlag: 'Y'
        },
        busPageInfo: {
        }
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    displayDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.show(name)
      })
    },
    hiddenDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.hide(name)
      })
    },
    addData() {
      this.isAdd = true
      const data = { paramBean: {}}
      sysMsg.toAdd(data).then(res => {
        this.displayDialog()
        this.form = res.data
      }).catch(err => {
        console.log(err)
      })
    },
    copyData(mainId) {
      this.isAdd = true
      const data = { paramBean: { id: mainId }}
      sysMsg.toCopy(data).then(res => {
        this.displayDialog()
        this.form = res.data
      }).catch(err => {
        console.log(err)
      })
    },
    viewMainData(id) {
      this.isAdd = false
      const data = { paramBean: { id: id }}
      sysMsg.viewMainData(data).then(res => {
        this.displayDialog()
        this.form = res.data
      })
    },
    doSubmit() {
      this.doSingleSave(true)
    },
    doSave() {
      this.doSingleSave(false)
    },
    doSingleSave(isSubmit) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.formSubmit = isSubmit
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      sysMsg.addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      sysMsg.addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.hiddenDialog()
      this.$refs['form'].resetFields()
    }
  }
}
</script>
